const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    index: null,
    picker: ['酒水饮料', '糖果饼干', '医疗保健', '数码家电', '母婴玩具'],
    imgList: [],
    modalName: null,
    itemValue: null,
    textareaAValue: '',
    textareaBValue: '',
    unit: "选择",
    scode: "",
    goodsName: "",
    simpleName: "",
    location: "",
    retailPrice: "",
    costPrice: "",
    quantity: "",
    className: "",
    items: [{
        name: 'ge',
        value: '个'
      },
      {
        name: 'zhi',
        value: '只'
      },
      {
        name: 'bao',
        value: '包'
      },
      {
        name: 'tiao',
        value: '条'
      },
      {
        name: 'ba',
        value: '把'
      },
      {
        name: 'zhi',
        value: '支'
      },
    ],
    unit: null
  },

  onLoad() {

    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    var that = this;
    
    that.getClass();
    console.log(that.data.classList)

    wx.request({
      url: "http://47.106.145.141:8080/api/v1/type",
      method: "GET",
      data: {
        "token": app.globalData.key,
        "shopId": app.globalData.shopId,
      },
      success: function (result) {
        console.log(result.data.dataInfo);
        that.setData({
          picker: result.data.dataInfo,
        })
      }
    })


    wx.hideLoading();

  },

  getClass: function () {
    var that = this;
    wx.request({
      url: "http://47.106.145.141:8080/api/v1/type",
      method: "GET",
      data: {
        "token": app.globalData.key,
        "shopId": app.globalData.shopId,
      },
      success: function (result) {
        console.log(result)
        that.setData({
          classList: result.data.dataInfo
          })       
      }
    })
  },

  PickerChange(e) {
    
    this.setData({
      typeId: this.data.classList[e.detail.value].id,
      index: e.detail.value
    })
    console.log(this.data.typeId);
  },
  MultiChange(e) {
    this.setData({
      multiIndex: e.detail.value
    })
  },
  ChooseImage() {
    wx.chooseImage({
      count: 1, //默认1
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
      }
    });
  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '删除图片',
      content: '确定要删除这张图片吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },
  textareaAInput(e) {
    this.setData({
      textareaAValue: e.detail.value
    })
  },
  textareaBInput(e) {
    this.setData({
      textareaBValue: e.detail.value
    })
  },
  showModal(e) {
    this.setData({

      modalName: e.currentTarget.dataset.target,

    })
  },
  hideModal(e) {
    this.setData({
      modalName: null,

    })
  },
  ChooseCheckbox(e) {
    let items = this.data.checkbox;
    let values = e.currentTarget.dataset.value;
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      if (items[i].value == values) {
        items[i].checked = !items[i].checked;
        break
      }
    }
    this.setData({
      checkbox: items
    })
  },
  //提交表单，存储数据
  formSubmit: function(e) {
    console.log( e.detail.value);
    var that = this;
    var res;

    wx.uploadFile({
      url: 'http://47.106.145.141:8080/api/v1/goods',
      filePath: that.data.imgList[0],
      name: 'file',
      formData: {
        "token": app.globalData.key,
        "empId": app.globalData.userId,
        "fullName": e.detail.value.goodsName,
        "shortForm": e.detail.value.simpleName,
        "quantity": e.detail.value.quantity,
        "type": that.data.typeId,
        "shopId": "832824",
        "primePrice": e.detail.value.costPrice,
        "retailPrice": e.detail.value.retailPrice,
        "barCode": e.detail.value.scode,
        "unit": that.data.unit
      },
      success: function (res) {
        res = JSON.parse(res.data);
        console.log(res);
        if(res.code==1){
          wx.showModal({
            title: '成功',
            content: '添加成功',
            showCancel:false,
            success:function(res) {
              var pages = getCurrentPages();
              var goodpage = pages[pages.length - 2];

              goodpage.setData({
                Refresh: true,
              })
              console.log(goodpage.data.Refresh);
              wx.navigateBack({
                delta: 1
              })
            }
          })
        }
      }
    })   

  },




  radioChange: function(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);
    this.setData({
      unit: e.detail.value
    })
  },
  //扫条码
  scan:function(e){

  }
})